<?php

namespace addons\booking\controller;
use addons\booking\model\Store as StoreModel;
use think\Db;

class StoreFinance extends Base
{
    protected $noNeedLogin = ['withdrawalAPP'];
    /**
     * 提现申请
     */
    public function withdrawalAPP()
    {
        if ($this->request->isPost()) {
            $bankcard = $this->request->param('bankcard');
            $bankname = $this->request->param('bankname');
            $money = $this->request->param('money');
            $province = $this->request->param('province');
            $userid = $this->auth->id;
            $userinfo = StoreModel::where('user_id', $userid)->find();
            $money=sprintf("%.2f",$money);
            if ($userinfo['money'] < $money) {
                $this->error('金额不足');
            }
            Db::startTrans();
            try {
                $storemoney = $userinfo['money'] -$money;
                StoreModel::where('id', $userinfo['id'])->update(['money'=>$storemoney]);
                //记录到提现表
                Db::name('withdrawal')->insert([
                    'user_id' => $userid,
                    'bankcard' => $bankcard,
                    'bankname' => $bankname,
                    'province' => $province,
                    'money'=>$money,
                    'createtime' => time(),
                ]);
                //记录到流水
                DB::name('user_money_log')->insert([
                    'user_id'=>$userid,
                    'money'=>$money,
                    'before'=>$userinfo['money'],
                    'after'=>$storemoney
                ]);
                Db::commit();
                $res=1;
            } catch (\Exception $e) {
                Db::rollback();
                $res=2;
            }
            if($res=1){
                $this->success('提现申请成功');
            }
            $this->error('提现申请失败');

        }
        $this->error('请求失败');
    }
    /**
     * 提现记录
     */
    public function withdrawalRecords()
    {
        $userid=$this->auth->id;
        $pageNum = 15;
        $param['num']=$this->request->param('num');
        if (isset($param['num']) && !empty($param['num'])) {
            $pageNum = $param['num'];
        }
        $list=DB::name('withdrawal')
            ->field('money,bankname,auditetime,status,fail_reason')
            ->where('user_id',$userid)
            ->paginate($pageNum)
            ->toArray();
        foreach ($list['data'] as $k=>$v){
            switch ($v['status']){
                case 1;
                    $list['data'][$k]['statusMsg']='审核中';
                    break;
                case  2;
                    $list['data'][$k]['statusMsg']='提现成功';
                    break;
                case 3;
                    $list['data'][$k]['statusMsg']='提现失败';
            }
        }
        $this->success('成功',$list);
    }
}